﻿Ext.define('Oa.TrainNeedGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'Widget.TrainNeedGrid',

    // override
    initComponent: function () {
        var store = Ext.create('Ext.data.TreeStore', {
            model: 'TreeModel',
            proxy: {
                type: 'ajax',
                url: '/dept.mvc/DeptTreeByEmp',
                extraParams: {
                    controllerName: 'TrainNeed'
                },
                reader: {
                    type: 'json'
                }
            },
            // 设置根节点
            root: {
                expanded: true
            },
            autoLoad: true
        });

        this.columns = [
            {
                header: "表单编号", width: 140,
                dataIndex: 'FormNo'
            }, {
                header: "申请部门",flex:1,
                dataIndex: 'DeptName'
            }, {
                header: "申请人", flex: 1,
                dataIndex: 'Name'
            }, {
                header: "最后更新时间", flex: 1,
                dataIndex: 'LastUpdateDate'
            }, { header: "操作", dataIndex: 'FormNo', width: 140, renderer: function (value, a, reader, c, d, ds) {
                var links = [];
                var hasSign = reader.data.StatusId != "0";
                links.push('<a href="#" onclick="return SignWin(\'' + value + '\',true,' + hasSign + ');">查看</a>');
                if (reader.data.EmpId == Cookies.get("empId") && "0,3".indexOf(reader.data.StatusId) >= 0) {
                    links.push('<a href="#" onclick="var url =\'MeetingRoomEdit.aspx?formNo=' + value + '\';window.showModalDialog(url, window, \'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;\');Ext.getCmp(\'MeetingRoomMgr\').getStore().reload();">编辑</a>');
                }
                if (reader.data.EmpId == Cookies.get("empId") && "0,3".indexOf(reader.data.StatusId) >= 0) {
                    links.push('<a href="#" onclick="return delForm(\'' + value + '\');">删除</a>');
                }
                return links.join("&nbsp;");
            }
            }
		];
        // Set up a model to use in our Store
        Ext.define('TrainNeedModel', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'FormNo'
            }, {
                name: 'DeptName'
            }, {
                name: 'EmpId'
            }, {
                name: 'Name'
            }, {
                name: 'LastUpdateDate'
            }, {
                name: 'StatusId'
            }]
        });

        var ds = Ext.create('Ext.data.Store', {
            model: 'TrainNeedModel',
            pageSize: 20, // items per page
            proxy: {
                type: 'ajax',
                url: '/TrainNeed.mvc/Find',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'//分页使用
                }
            },
            autoLoad: false
        });
        ds.load({ params: { start: 0, limit: 20} });
        ds.on('beforeload', function (thiz, options) {
            Ext.apply(thiz.proxy.extraParams, { idStr: Ext.getCmp('treeDeptTrainNeed').getValue() });
        });
        this.store = ds;

        this.dockedItems = [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                xtype: 'tbtext',
                text: '部门:'
            }, {
                xtype: 'treepicker',
                id: 'treeDeptTrainNeed',
                displayField: 'text',
                forceSelection: true,
                editable: false,
                width: 250,
                store: store
            }, {
                text: "查询",
                iconCls: 'icon-search',
                handler: function () {
                    Ext.getCmp('pagingTrainNeed').moveFirst();
                }
            }, '-', {
                text: "添加",
                tooltip: "添加新的信息",
                iconCls: "addicon",
                handler: function () {
                    var url = 'MeetingRoomEdit.aspx';
                    var retVal = window.showModalDialog(url, window, 'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;');
                    if (retVal != undefined && retVal.indexOf('Form') >= 0) {
                        ds.reload();
                    }
                }
            }]
        }, {
            xtype: 'pagingtoolbar',
            id: 'pagingTrainNeed',
            store: ds,
            dock: 'bottom',
            displayInfo: true
        }];

        // finally call the superclasses implementation
        Oa.TrainNeedGrid.superclass.initComponent.call(this);
    }
});